﻿using System;
using MySql.Data.MySqlClient;

namespace KoDService
{
    public class Database
    {
        public MySqlConnection dbConn = new MySqlConnection("server=216.218.192.170;Port=3306;User Id=qsefthuk;Password=nicolastyler;Persist Security Info=True;database=qsefthuk_KoD");

        public bool openDB()
        {
            try
            {
                if (dbConn.State == System.Data.ConnectionState.Closed)
                    dbConn.Open();
                return true;
            }
            catch (MySqlException)
            {
                return false;
            }
        }

        public void closeDB()
        {
            if (dbConn.State == System.Data.ConnectionState.Open)
                dbConn.Close();
        }

        public MySqlDataReader ExecuteQuery(string strSql)
        {
            try
            {
                if (dbConn.State == System.Data.ConnectionState.Closed)
                    openDB();
                MySqlCommand cmd = new MySqlCommand(strSql, dbConn);
                if (!strSql.Contains("SELECT"))
                {
                    cmd.ExecuteNonQuery();
                    return null;
                }
                else
                {
                    MySqlDataReader sqlReader = cmd.ExecuteReader();
                    return sqlReader;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
